﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace cebao.Web.ForsetGrass
{
    /// <summary>
    /// GetGrass 的摘要说明
    /// </summary>
    public class GetGrass : IHttpHandler, IRequiresSessionState
    {
        cebao.BLL.ForsetGrass bll = new BLL.ForsetGrass();
        StringBuilder result = new StringBuilder();

        StringBuilder sb = new StringBuilder();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "queryh")
                Queryh(context);
            else if (ac == "queryhc")
                Queryhc(context);
            else if (ac == "queryc")//汇总
                Queryc(context);
            else if (ac == "mypet")//我的虫子列表
                Querymy(context);

            else if (ac == "del")//汇总所有有害生物
                delete(context);

        }
        public void Queryhc(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", otype = "0", mlevel = "";
                string idstr = "";
                string areaid = "";
                string id = "";
                string idh = "";
                int ulevel = Web.Admin.myinfo.getLevel(context);
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["mlevel"])
                {//获取前台传来的值  
                    mlevel = context.Request.Form["mlevel"].ToString().Trim();
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");
                if (null != context.Request.Form["idh"])
                    idh = context.Request.Form["idh"];

                if (null != context.Request.Form["idstr"] && context.Request.Form["idstr"].ToString().Trim() != "")
                {
                    idstr = context.Request.Form["idstr"].ToString().Trim();
                    areaid = idstr;
                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" inputTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" inputTime<='{0}' and ", EndTime);
                }
                if (idh != "")
                {
                    strWhere.AppendFormat(" mhid in ({0}) and ", idh);
                }
                if (ulevel != 0)
                {
                    if (areaid != "")
                    {
                        strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                    }
                }
                else
                {
                    strWhere.AppendFormat(" uid={0} and ", uid);

                }
                if (mlevel != "" && mlevel != "4,3,2,1")
                {
                    strWhere.AppendFormat(" mlevel in ({0}) and ", mlevel);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int level = Web.Admin.myinfo.getLevel(context);
            if (level == 5 || level == 1)
            {
                string uidstr = context.Request.QueryString["ids"];
                if (bll.DeleteList(uidstr))
                {
                    context.Response.Write("[{\"r\":\"T\"}]");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("[{\"r\":\"F\"}]");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("[{\"r\":\"F\"}]");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int suid = 0;
                if (null != context.Request.QueryString["uid"])
                    suid = int.Parse(context.Request.QueryString["uid"]);
                DataSet ds = bll.GetListdis(suid);
                string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }
        public void Queryc(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", otype = "0",mlevel="";
                string idstr = "";
                string areaid = "";
                if (null != context.Request.Form["mlevel"])
                {//获取前台传来的值  
                    mlevel = context.Request.Form["mlevel"].ToString().Trim();
                }

                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["otype"])
                {//获取前台传来的值  
                    otype = context.Request.Form["otype"].ToString().Trim();
                }
                if (null != context.Request.Form["city"])
                    areaid = context.Request.Form["city"];
                //如果地区为空就取用户自己的区域
                if (areaid == "")
                {
                    BLL.areainfo blla = new BLL.areainfo();
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                        areaid = blla.GetModel(aid).abh;
                    else
                        areaid = "000000";

                }

                DataSet ds = null;
                if (otype == "1")
                {
                    idstr = context.Request.Form["idh"];

                    ds = bll.GetListcc(areaid, idstr, StartTime, EndTime, mlevel);


                }
                else
                {
                    ds = bll.GetListcarea(areaid, "", StartTime, EndTime, mlevel);

                }
                if (ds != null)
                {
                    int count = ds.Tables[0].Rows.Count;//获取条数  
                    string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                    context.Response.Write(strJson);//返回给前台页面 
                }
                else
                {
                    context.Response.Write("");
                }

                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Queryh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", otype = "0",mlevel="";
                string idstr = "";
                string areaid = "";
                int id = 0;
                int idh = 0;
                if (null != context.Request.Form["mlevel"])
                {//获取前台传来的值  
                    mlevel = context.Request.Form["mlevel"].ToString().Trim();
                }
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["otype"])
                {//获取前台传来的值  
                    otype = context.Request.Form["otype"].ToString().Trim();
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");
                if (otype == "1")
                {
                    if (null != context.Request.Form["idh"])
                        idh = int.Parse(context.Request.Form["idh"]);
                }
                if (otype == "2")
                {
                    if (null != context.Request.Form["idstr"])
                    {
                        idstr = context.Request.Form["idstr"].ToString().Trim();
                        if (idstr != "")
                        {
                            id = int.Parse(idstr);
                            //根据点击ID找下面所有ID
                            result.Remove(0, result.Length);
                            areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                        }
                    }

                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" inputTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" inputTime<='{0}' and ", EndTime);
                }
                if (otype == "1")
                {
                    strWhere.AppendFormat(" mhid in ({0}) and ", idh);
                }
                int ulevel = Web.Admin.myinfo.getLevel(context);
                if (ulevel != 0)
                {
                    if (areaid != "")
                    {
                        strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                    }
                }
                else
                {
                    strWhere.AppendFormat(" uid={0} and ", uid);

                }
                if (mlevel != "" && mlevel != "4,3,2,1")
                {
                    strWhere.AppendFormat(" mlevel in ({0}) and ", mlevel);

                } 
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", mypet = "";
                int suid = 0, mypetid = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["mypet"])
                {//获取前台传来的值  
                    mypet = context.Request.Form["mypet"].ToString().Trim();
                    mypetid = int.Parse(mypet);
                }
                if (null != context.Request.Form["uid"])
                    suid = int.Parse(context.Request.Form["uid"]);
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" inputTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" inputTime<='{0}' and ", EndTime);
                }
                if (mypetid != 0)
                {
                    strWhere.AppendFormat(" mhid='{0}' and ", mypetid);
                }
                strWhere.AppendFormat(" uid='{0}' ", suid);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }

        private string GetIDByTable(DataTable tabel, string idCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("," + row[idCol]);
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            GetIDByTable(tabel, idCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }
                }
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}